package com.akkainaction.tool.future;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.Patterns;
import akka.util.Timeout;
import com.akkainaction.tool.future.actor.FutureActor;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import java.util.concurrent.TimeUnit;

public class TestFuture {

    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("sys");
        ActorRef futureActor = system.actorOf(Props.create(FutureActor.class), "futureActor");
        Timeout timeout = new Timeout(Duration.create(3, TimeUnit.SECONDS));
        Future<Object> future = Patterns.ask(futureActor, "hello", timeout);
        try {
            // 同步api，也就是说只有接收到消息之后，才会继续往下执行。
            String replymsg = (String) Await.result(future, timeout.duration());
            System.out.println("收到返回消息:[" + replymsg + "]");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
